package cn.edu.hitwh.yibao.service.impl;

import cn.edu.hitwh.yibao.entity.MedicalInstitution;
import cn.edu.hitwh.yibao.form.MedicalInstitutionForm;
import cn.edu.hitwh.yibao.mapper.MedicalInstitutionMapper;
import cn.edu.hitwh.yibao.service.MedicalInstitutionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import java.util.List;

@Service
public class MedicalInstitutionServiceImpl implements MedicalInstitutionService {
    @Autowired
    private MedicalInstitutionMapper medicalInstitutionMapper;

    @Override
    public String select(MedicalInstitutionForm medicalInstitutionForm, Model model) {
        MedicalInstitution medicalInstitution = new MedicalInstitution();
        BeanUtils.copyProperties(medicalInstitutionForm, medicalInstitution);
        List<MedicalInstitution> list = medicalInstitutionMapper.selectList(medicalInstitution);
        model.addAttribute("medInsList", list);
        return "/medicalInstitution/select";
    }

    @Override
    public String add(MedicalInstitutionForm medicalInstitutionForm, Model model) {
        MedicalInstitution medicalInstitution = new MedicalInstitution();
        BeanUtils.copyProperties(medicalInstitutionForm, medicalInstitution);
        int count = medicalInstitutionMapper.insert(medicalInstitution);
        if (count > 0) {
            return "redirect:/medicalInstitution/addComplete";
        } else {
            model.addAttribute("addResult", "添加定点医疗机构失败！");
            return "/medicalInstitution/select";
        }
    }

    @Override
    public String updateInit(String diaId, Model model) {
        MedicalInstitution medicalInstitution = medicalInstitutionMapper.selectById(diaId);
        model.addAttribute("medIns", medicalInstitution);
        return "/medicalInstitution/update";
    }

    @Override
    public String update(MedicalInstitutionForm medicalInstitutionForm, Model model) {
        MedicalInstitution medicalInstitution = new MedicalInstitution();
        BeanUtils.copyProperties(medicalInstitutionForm, medicalInstitution);
        int count = medicalInstitutionMapper.update(medicalInstitution);
        if (count > 0) {
            return "redirect:/medicalInstitution/updateComplete";
        } else {
            return "/medicalInstitution/select";
        }
    }

    @Override
    public String delete(String diaId, Model model) {
        int count = medicalInstitutionMapper.delete(diaId);
        if (count > 0) {
            return "redirect:/medicalInstitution/deleteComplete";
        } else {
            model.addAttribute("deleteResult", "删除定点医疗机构失败！");
            return "/medicalInstitution/select";
        }
    }

    @Override
    public String detail(String diaId, Model model) {
        MedicalInstitution medicalInstitution = medicalInstitutionMapper.selectById(diaId);
        model.addAttribute("medIns", medicalInstitution);
        return "/medicalInstitution/detail";
    }
}